package com.tgy.thread;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * @Author: tgy
 * @Date: 12/2/20 3:16 PM
 */
public class SemaphoreDemo {

    public static void main(String[] args) {

        Semaphore semaphore = new Semaphore(5);


        for (int i = 0; i < 50; i++) {

            new Thread(()->{

                try {
                    semaphore.acquire();
                    System.out.println("do thing");
                    TimeUnit.SECONDS.sleep(1);
                    semaphore.release();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            },"t" + i).start();
        }
    }
}
